package com.leopardcat.firewall;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SMSBlackListDB extends SQLiteOpenHelper {
	private final static String DATABASE_NAME = "sms_black_list.db";
	private final static String TABLE_NAME = "sms_black_list";
	private final static String NUMBER_ID  = "id";
	private final static String NUMBER_NAME = "number";
	private final static int DATABASE_VERSION = 1;
	
	public SMSBlackListDB(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create blacklist table
		String sql = "CREATE TABLE " + TABLE_NAME + 
		 " (" + NUMBER_ID + " INTEGER primary key autoincrement,"
		 + NUMBER_NAME + " text);";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
		db.execSQL(sql);
		onCreate(db);
		
	}
	
	public Cursor select() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		return cursor;
	}
	
	public long insert(String number) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(NUMBER_NAME, number);
		long row = db.insert(TABLE_NAME, null, cv);
		return row;
	}
	
	public void delete(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = NUMBER_ID + " = " + Integer.toString(id);
		//String[] whereValue = {Integer.toString(id)};
		db.delete(TABLE_NAME, where, null);
	}
	
	public void delete(String number) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = NUMBER_NAME + "=" + number;
		//String[] whereValue = {number};
		db.delete(TABLE_NAME, where, null);
	}
	
}
